<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\facade\WeChat;
use app\common\facade\WeChatMini;
use app\common\model\User;
use app\common\model\User as UserModel;
use app\common\tool\Result;
use think\exception\ValidateException;
use think\Response;
use WeChat\Contracts\BasicWeChat;
use WeMini\Crypt;

class Login extends Base
{

    /**
     * 原点 2022年2月16日14:16:31 小程序授权登录
     */
    public function miniAppletLogin(): Response
    {
        try {

            $param = $this->request->post();
            $this->validate($param, 'MiniAppletLogin');
            $result = (new Crypt(config('pay')['wechat']))->session($param['code']);

            $result['nickname'] = $param['nickname'];
            $result['photo'] = $param['photo'];
            return json(User::_miniAppletSave($result));

        } catch (ValidateException $e) {
            return json(['code' => -1, 'msg' => $e->getError()]);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * post: 登录注册
     * path: Login
     * method: Login
     * param: code - {string} code
     */
    public function Login()
    {
        //首先 use app\common\facade\WeChat;
        try {
            //获取openid，一般只做微信公众号JSAPI用到
            $post = $this->request->post();
//            $result = WeChatMini::getPhoneNumber($post['code']);
//            var_dump($result);die;
            $result = WeChatMini::session($post['code']);
            $where['openid'] = $result['openid'];
            $user = User::where($where)->find();
            if($user)
            {
                $time = time();
                $token = md5(json_encode($user) . $time . $this->request->ip());
                $user->token = $token;
                $user->last_time = $time;
                $user->last_ip = $this->request->ip();
                $user->save();
            }else{
                $param['code'] = generate(8, '', 1);
                $param['openid'] = $result['openid'];
                $param['last_time'] = time();
                $param['last_ip'] = $this->request->ip();
                $token = md5(json_encode($user) . time() . $this->request->ip());
                $param['token'] = $token;
                if(isset($post['parent_code']))
                {
                    $parent = User::where(['code' => $post['parent_code'], 'is_del' => 0])->find();
                    if (empty($parent)) {
                        return ['code' => -1, 'msg' => '推荐人不存在或未激活'];
                    }
                    $param['parent_code'] = $post['parent_code'];
                    $param['parent_id'] = $parent->id;
                    $param['parent_name'] = $parent->nickname;
                }

                User::create($param);
            }
            $data['token'] = $token;

            return Result::success('登录成功', $data);
//            var_dump($result);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }

    }

}
